package demo5;

import java.util.Random;
import java.util.Timer;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 33013
 * Date: 2023-09-03
 * Time: 22:35
 */

class Sum{
    public int sum1 = 0;
    public int sum2 = 0;

    public Sum(){
        ;
    }

    public int getsum(int[] ints, int x) throws InterruptedException {
        if (x == 0){
            Thread thread = new Thread(() -> {
                for (int i = x; i < ints.length; i+=2) {
                    sum1 += ints[i];
                }
            });
            thread.start();
            thread.join();
            return sum1;
        }else {
            Thread thread = new Thread(() -> {
                for (int i = x; i < ints.length; i+=2) {
                    sum2 += ints[i];
                }
            });
            thread.start();
            thread.join();
            return sum2;
        }
    }

}

public class Test {
//    public static void main(String[] args) throws InterruptedException {
//        long start = System.currentTimeMillis();
//        Random random = new Random();
//        int[] ints = new int[10000000];
//
//        for (int i = 0; i < ints.length; i++) {
//            ints[i] = random.nextInt(100) + 1;
//        }
//        Sum sum = new Sum();
//        int amount = 0;
//        amount += sum.getsum(ints, 1) + sum.getsum(ints,0);
//        System.out.println(amount);
//        long end = System.currentTimeMillis();
//        System.out.println(end - start);
//    }
    public static int count = 0;
    private static Object object = new Object();
    public static void main(String[] args) throws InterruptedException {
          Thread thread1 = new Thread(() -> {
              for (int i = 0; i < 10000; i++) {
                  synchronized (object){
                      count++;
                  }
              }
          });
          Thread thread2 = new Thread(() -> {
              for (int i = 0; i < 10000; i++) {
                  synchronized (object){
                      count++;
                  }
              }
          });
          thread1.start();
          thread2.start();
          thread1.join();
          thread2.join();
          System.out.println(count);
    }
}
